package com.gohome.gohomechat.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Friend;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by dllo on 16/3/1.
 */
public class AddressListViewAdapter extends BaseAdapter {
    private List<Friend> datas;
    private Context context;

    public AddressListViewAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<Friend> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return datas!=null&&datas.size()>0?datas.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.adress_listview_item,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        Friend friend = datas.get(position);
        String word = friend.getPinyin().substring(0, 1).toUpperCase();

       // word.toUpperCase();// 字符串小写转大写
        holder.address_listview_item_word.setText(word);
        holder.address_listview_item_username.setText(friend.getUserName());
        Picasso.with(context).load(friend.getHead()).into(holder.address_listview_item_head);
        //下标为0的显示
        if(position==0) {
            holder.address_listview_item_word.setVisibility(View.VISIBLE);
        } else {
            //取出上一个friend, 并得到的第一个word
            String preWord = datas.get(position - 1).getPinyin().substring(0, 1).toUpperCase();;
           // preWord.toUpperCase();
            //判断是否于当前行的word是否相同
            //如果相同, 隐藏
            if (word.equals(preWord)) {
                holder.address_listview_item_word.setVisibility(View.GONE);
            } else {
                //如果不同, 显示
                holder.address_listview_item_word.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    class ViewHolder {
        private TextView address_listview_item_word,address_listview_item_username;
        private ImageView address_listview_item_head;
        public ViewHolder(View view) {
            address_listview_item_word= (TextView) view.findViewById(R.id.address_listview_item_word);
            address_listview_item_username= (TextView) view.findViewById(R.id.address_listview_item_username);
            address_listview_item_head= (ImageView) view.findViewById(R.id.address_listview_item_head);
        }
    }
}
